package ca.ericslandry.client.mvp.view;

import ca.ericslandry.client.MapsUtils;
import ca.ericslandry.client.mvp.presenter.UserPresenter;
import ca.ericslandry.client.resource.MyConstants;
import ca.ericslandry.client.widget.LocationPicker;
import ca.ericslandry.shared.Constants;
import ca.ericslandry.shared.domain.User;
import ca.ericslandry.shared.util.GeoHash;

import com.google.appengine.api.datastore.Email;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserView extends BaseView implements UserPresenter.Display {
	private final TextBox displayNameField = new TextBox();
	private final TextBox realNameField = new TextBox();
	private final TextBox emailField = new TextBox();
	private final LocationPicker geohashField = new LocationPicker();
	private final Button saveButton = new Button(MyConstants.INSTANCE.save());
	private final VerticalPanel mainPanel = new VerticalPanel();
	private User user;

	public UserView() {
		mainPanel.setSpacing(4);
	}

	@Override
	public Widget asWidget() {
		return mainPanel;
	}

	@Override
	public HasClickHandlers getSave() {
		return saveButton;
	}

	@Override
	public User getUser() {
		user.setDisplayName(displayNameField.getValue());
		user.setRealName(realNameField.getValue());
		user.setEmail(new Email(emailField.getValue()));
		user.setGeohash(geohashField.getValue());
		return user;
	}

	@Override
	public void editUser(User user) {
		this.user = user;
		mainPanel.clear();
		Label label = new Label(MyConstants.INSTANCE.updateUserHeader());
		label.setStyleName(Constants.CSS.HEADER);
		mainPanel.add(label);
		Label details = new Label(MyConstants.INSTANCE.userDetails());
		details.setStyleName(Constants.CSS.SECTION_HEADER);
		mainPanel.add(details);
		FlexTable table = new FlexTable();
		table.setWidget(0, 0, new HTML(MyConstants.INSTANCE.identity()));
		TextBox identityField = new TextBox();
		identityField.setValue(user.getOpenID());
		identityField.setReadOnly(true);
		table.setWidget(0, 1, identityField);
		table.setWidget(1, 0, new HTML(MyConstants.INSTANCE.displayName()));
		if (!user.getDisplayName().startsWith("?")) {
			displayNameField.setValue(user.getDisplayName());
		}
		table.setWidget(1, 1, displayNameField);
		table.setWidget(2, 0, new HTML(MyConstants.INSTANCE.realName()));
		realNameField.setValue(user.getRealName());
		table.setWidget(2, 1, realNameField);
		VerticalPanel emailPanel = new VerticalPanel();
		emailPanel.add(new HTML(MyConstants.INSTANCE.email()));
		HTML gravatarInfo = new HTML("Used for <a href=\"http://gravatar.com\">Gravatar</a> icon.");
		gravatarInfo.setStyleName(Constants.CSS.FOOTNOTE);
		emailPanel.add(gravatarInfo);
		table.setWidget(3, 0, emailPanel);
		emailField.setValue(user.getEmail().getEmail());
		table.setWidget(3, 1, emailField);
		mainPanel.add(table);
		Label locationLabel = new Label(MyConstants.INSTANCE.pickLocation());
		locationLabel.setStyleName(Constants.CSS.SECTION_HEADER);
		mainPanel.add(locationLabel);
		geohashField.setValue(user.getGeohash());
		mainPanel.add(geohashField);
		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.setSpacing(15);
		buttonPanel.add(saveButton);
		mainPanel.add(buttonPanel);
	}

	private boolean thisUserIsMyself(User user) {
		return user != null && UserPresenter.currentUser != null
				&& UserPresenter.currentUser.getOpenID().equals(user.getOpenID());
	}

	@Override
	public void viewUser(User user) {
		mainPanel.clear();
		Label label = new Label(MyConstants.INSTANCE.userInformation());
		label.setStyleName(Constants.CSS.HEADER);
		mainPanel.add(label);
		FlexTable table = new FlexTable();
		if (thisUserIsMyself(user)) {
			table.setWidget(0, 0, new HTML(MyConstants.INSTANCE.identity()));
			table.setWidget(0, 1, new HTML(user.getOpenID()));
		}
		table.setWidget(1, 0, new HTML(MyConstants.INSTANCE.displayName()));
		String displayName = user.getDisplayName();
		if (displayName == null) {
			displayName = MyConstants.INSTANCE.unknown();
		} else if (displayName.startsWith("?")) {
			displayName = MyConstants.INSTANCE.unknown() + displayName.substring(1);
		}
		table.setWidget(1, 1, new HTML(displayName));
		mainPanel.add(table);
		if (user.getGeohash() != null) {
			GeoHash geohash = GeoHash.fromGeohashString(user.getGeohash());
			LatLng point = geohash.getPoint().toLatLng();
			mainPanel.add(MapsUtils.getLittleMapWidget(point));
		}
		if (thisUserIsMyself(user)) {
			mainPanel.add(UserPresenter.getLinkEditUser(user));
		}
	}
}
